package cn.pid21.client.request;

import cn.hutool.core.codec.Base64;

import java.util.HashMap;
import java.util.Map;

/**
 * PID 请求头信息
 *
 * @author 郭志斌
 * @version 1.0.0
 * <b>Creation Time:</b> 2019/8/13 17:14
 */
public class DataReportRequestHeader {

    /**
     * 请求头信息
     */
    private Map<String,String> header;

    private DataReportRequestHeader(Map<String,String> header){
        this.header = header;
    }

    /**
     * 请求头参数数量
     */
    private static final int PID_REQUEST_HEADER_LENGTH = 3;

    DataReportRequestHeader(){
        this(new HashMap<>(PID_REQUEST_HEADER_LENGTH));
    }

    /**
     * 创建 PidRequestHeader 实例
     *
     * @return PidRequestHeader
     */
    public static DataReportRequestHeader builder(){
        return new DataReportRequestHeader();
    }

    /**
     * 创建 PidRequestHeader 实例
     *
     * @return PidRequestHeader
     */
    public static DataReportRequestHeader build(Map<String,String> header){
        return new DataReportRequestHeader(header);
    }

    /**
     * 设置 clientId 属性值
     *
     * @param clientId 客户端ID
     * @return PidRequestHeader
     */
    public DataReportRequestHeader clientId(String clientId){
        header.put("clientId",clientId);
        return this;
    }

    /**
     * 设置 secret 属性值
     *
     * @param secret 秘钥
     * @return PidRequestHeader
     */
    public DataReportRequestHeader secret(String secret){
        header.put("secret",secret);
        return this;
    }

    /**
     * 设置 Http Basic 验证请求头
     *
     * @param clientId
     * @param secret
     * @return
     */
    public DataReportRequestHeader httpBasic(String clientId, String secret){
        String basic = "Basic " + Base64.encodeUrlSafe(clientId + ":" + secret);
        header.put("Authorization",basic);
        return this;
    }

    /**
     * 设置 sign 属性值
     *
     * @param sign 签名
     * @return Map<String, String>
     */
    public Map<String,String> sign(String sign){
        header.put("sign",sign);
        return header;
    }

    /**
     * 获取 Header
     *
     * @return Map<String, String>
     */
    public Map<String, String> getHeader() {
        return header;
    }

    /**
     * 获取 clientId
     *
     * @return String
     */
    public String getClient(){
        return header.get("clientId");
    }

    /**
     * 获取秘钥
     *
     * @return String
     */
    public String getSecret(){
        return header.get("secret");
    }

    /**
     * 获取签名
     *
     * @return String
     */
    public String getSign(){
        return header.get("sign");
    }

}
